
package group3equationsolver;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
 * 
 * Make a GUI for the program, use to interact with user
 * 
 * @author Group 3
 * @member Nguyễn Hùng Phi
 * @member Hoàng Văn Hậu
 * @member Thái Trọng Hiếu
 * @member Dương Dinh Phúc
 */
public class SolverGUI {
	//declare window view
	JFrame window = new JFrame();
	private JTextField textA;		//declare text field for input A
	private JTextField textB;		//declare text field for input B		
	private JTextField textC;		//declare text field for input C
	private JTextArea textAnswer;	//declare text area for output answer string
	private JButton btnSolve;		//declare solve button
	
	//GUI configuration class
	public SolverGUI(){
		
		//configure window size
		window.setSize(286, 303);
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setBackground(Color.CYAN);
		window.setTitle("Equation Solver");
		window.getContentPane().setLayout(null);
		
		//configure textField A 
		textA = new JTextField();
		textA.setBounds(40, 11, 220, 20);
		window.getContentPane().add(textA);
		textA.setColumns(10);
		textA.setVisible(true);

		//configure textField B 
		textB = new JTextField();
		textB.setBounds(40, 42, 220, 20);
		window.getContentPane().add(textB);
		textB.setColumns(10);

		//configure textField C 
		textC = new JTextField();
		textC.setBounds(40, 73, 220, 20);
		window.getContentPane().add(textC);
		textC.setColumns(10);
		
		//Label for textField A
		JLabel lblA = new JLabel("A");
		lblA.setBounds(10, 14, 46, 14);
		window.getContentPane().add(lblA);
		
		//Label for textField C
		JLabel lblB = new JLabel("B");
		lblB.setBounds(10, 45, 46, 14);
		window.getContentPane().add(lblB);
		
		//Label for textField C
		JLabel lblC = new JLabel("C");
		lblC.setBounds(10, 76, 46, 14);
		window.getContentPane().add(lblC);
		
		//configure answer output field
		textAnswer = new JTextArea();
		textAnswer.setBounds(10, 139, 250, 58);
		window.getContentPane().add(textAnswer);
		textAnswer.setEditable(false);
		textAnswer.setWrapStyleWord(true);
		
		//configure solve button
		btnSolve = new JButton("Solve");
		btnSolve.setBounds(171, 230, 89, 23);
		window.getContentPane().add(btnSolve);
		
		//set window visible
		window.setVisible(true);
		
		//call method to add action listener for the solve button
		addActionListener();
	}
	
	/**
	 * This class use to add Action Listener to the button
	 * which will take the data from 3 textfield and solve, then output the string answer
	 */
	public void addActionListener(){
		btnSolve.addActionListener(new ActionListener() {
			
			//action perform method
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//declare a Solver object
				Solver solve = new Solver();
				//declare 3 variable for geting the inputed data
				double a;
				double b;
				double c;
				//get data from text field A
				try{
					a = Double.parseDouble(textA.getText());
				}catch(NumberFormatException e){
					//if any fomat wrong or empty, auto set a to 0
					textA.setText("0");
					a = 0;
				}

				//get data from text field B
				try{
					b = Double.parseDouble(textB.getText());
				}catch(NumberFormatException e){
					//if any fomat wrong or empty, auto set a to 0
					textB.setText("0");
					b = 0;
				}
				
				//get data from text field C
				try{
					c = Double.parseDouble(textC.getText());
				}catch(NumberFormatException e){
					//if any fomat wrong or empty, auto set a to 0
					textC.setText("0");
					c = 0;
				}
				
				//set the answer string to the output text area
				textAnswer.setText(solve.solve2(a, b, c));				
			}
		});
	}
	
	public static void main(String[] args) {
		SolverGUI s = new SolverGUI();
		s.addActionListener();
	}
}
